using System;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;
using System.Collections;

namespace ACEMPortal.Layer.Workflowlayer.DocumentManager
{
	public class XMLActivitiesWithAttributes : XMLActivities
	{
        string sOwner;

        public void Sort(String sSortExpression, bool bAscending)
        {
            this.InnerList.Sort(new ActivityComparer(sSortExpression));

            if (!bAscending)
                this.InnerList.Reverse();
        }

        private sealed class ActivityComparer : IComparer
        {
            private string sFieldComparer = String.Empty;
            public ActivityComparer(string _FieldComparer)
            {
                this.sFieldComparer = _FieldComparer;
            }
            public int Compare(object x, object y)
            {
                XMLActivityWithAttributes first = (XMLActivityWithAttributes)x;
                XMLActivityWithAttributes second = (XMLActivityWithAttributes)y;

                switch (sFieldComparer)
                {
                    case "IdDocument":
                        return first.IdDocument.CompareTo(second.IdDocument);
                    case "InsertedBy":
                        return first.InsertedBy.CompareTo(second.InsertedBy);
                    case "ActivityAlias":
                        return first.ActivityAlias.CompareTo(second.ActivityAlias);
                    case "ProcessAlias":
                        return first.ProcessAlias.CompareTo(second.ProcessAlias);
                    case "NowOwner":
                        return first.NowOwner.CompareTo(second.NowOwner);
                    case "ProcessId":
                        return first.ProcessId.CompareTo(second.ProcessId);
                    case "Name":
                        return first.Name.CompareTo(second.Name);
                    case "Date":
                        {
                            IFormatProvider culture = new System.Globalization.CultureInfo("it-IT", true);

                            DateTime dtFirst = DateTime.Parse(first.Date, culture);
                            DateTime dtSecond = DateTime.Parse(second.Date, culture);

                            return dtFirst.CompareTo(dtSecond);
                        }
                    default:
                        return first.Name.CompareTo(second.Name);
                }
            }
        }

        public XMLActivitiesWithAttributes(string sUser)
            : base()
        {
            sOwner = sUser;
        }

        public string RetrieveAttribute(XMLAttributes xmlAttributes, string sAttributeName)
        {
            string sValue = string.Empty;
            XMLAttribute attribute = null;
            try
            {
                attribute = (XMLAttribute)xmlAttributes.Get(sAttributeName);
                sValue = attribute.Value;
            }
            catch (System.Exception)
            { }

            return sValue;
        }

        public string RetrieveAttribute(XMLAttributes xmlAttributes, string sAttributeName, string sDefaultValue)
        {
            string sValue = string.Empty;
            XMLAttribute attribute = null;
            try
            {
                attribute = (XMLAttribute)xmlAttributes.Get(sAttributeName);
                sValue = attribute.Value;
            }
            catch (System.Exception)
            {
                return sDefaultValue;
            }

            return sValue;
        }

        public override void Parse(System.Xml.XmlNode nodeList)
        {
            this.Init();

            System.Xml.XmlNode node;

            for (int i = 0; i < nodeList.ChildNodes.Count; i++)
            {
                node = nodeList.ChildNodes[i];

                switch (node.Name)
                {
                    case XMLActivity.MAIN_NODE:
                        XMLActivityWithAttributes activity = new XMLActivityWithAttributes();
                        activity.Parse(node);

                        string sAttributes = string.Empty;
                        ScenarioFramework.WAPI.Response wapiResponse = new ScenarioFramework.WAPI.Response();
                        sAttributes = ScenarioFramework.WAPI.Process.GetProcessInstanceAttributes(ref wapiResponse, activity.ProcessId);

                        if (wapiResponse.iCode == 0)
                        {
                            XMLAttributes xmlAttributes = new XMLAttributes();
                            xmlAttributes.Parse(sAttributes);

                            activity.IdDocument = this.RetrieveAttribute(xmlAttributes, "IdDocument");
                            if (activity.IdDocument.CompareTo(string.Empty) == 0)
                            {
                                activity.IdDocument = this.RetrieveAttribute(xmlAttributes, "IdAM");
                            }

                            activity.InsertedBy = this.RetrieveAttribute(xmlAttributes, "User");
                            activity.ProcessAlias = this.RetrieveAttribute(xmlAttributes, "ProcessAlias");
                        }

                        if (activity.XMLLog.Status == XMLLog.RUNNING_STATUS)
                        {
                            sAttributes = ScenarioFramework.WAPI.WorkItem.GetWorkItemAttributes(activity.GetXML(), this.sOwner, ref wapiResponse);
                        }
                        else
                        {
                            sAttributes = ScenarioFramework.WAPI.Activity.GetActivityAttributes(activity.GetXML(), this.sOwner, ref wapiResponse);
                        }

                        if (wapiResponse.iCode == 0)
                        {
                            XMLAttributes xmlAttributes = new XMLAttributes();
                            xmlAttributes.Parse(sAttributes);

                            activity.ActivityAlias = this.RetrieveAttribute(xmlAttributes, "ActivityAlias", activity.Name);
                            if (activity.ActivityAlias == string.Empty)
                            {
                                activity.ActivityAlias = activity.Name;
                            }

                            activity.HelpUrl = this.RetrieveAttribute(xmlAttributes, "HelpUrl");

                        }

                        this.Add(activity);
                        break;
                }
            }
        }
    }
}
